package com.example.guiguihomemedical.service.impl;

import com.example.guiguihomemedical.entity.Doctor;
import com.example.guiguihomemedical.entity.FamilyarChives;
import com.example.guiguihomemedical.entity.Record;
import com.example.guiguihomemedical.entity.dto.FamilyarChivesList;
import com.example.guiguihomemedical.entity.dto.FamilyarChivesRecordList;
import com.example.guiguihomemedical.entity.dto.FamilyarSigning;
import com.example.guiguihomemedical.entity.dto.RecordList;
import com.example.guiguihomemedical.mapper.DoctorMapper;
import com.example.guiguihomemedical.mapper.FamilyarChivesMapper;
import com.example.guiguihomemedical.mapper.RecordMapper;
import com.example.guiguihomemedical.service.FamilyarChivesService;
import com.example.guiguihomemedical.service.RecordListService;
import com.example.guiguihomemedical.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

@Service
public class FamilyarChivesServiceImpl implements FamilyarChivesService {
    @Autowired
    private FamilyarChivesMapper familyarChivesMapper;
    @Autowired
    private RecordMapper recordMapper;
    @Autowired
    private RecordService getRecordList;
    @Autowired
    private RecordListService recordListService;
    @Autowired
    private DoctorMapper doctorMapper;

    @Override
    public  List<FamilyarChives> findByHouseholdId(String householdId) {
        return familyarChivesMapper.findByHouseholdId(householdId);
    }

    @Override
    public FamilyarChives findByMemberId(String memberId) {
        return familyarChivesMapper.findByMemberId(memberId);
    }

    @Override
    public void insertFamilyarchives(String householdId, String memberId) {
        recordMapper.updateRecordType(memberId,"2");
        familyarChivesMapper.insertFamilyarchives(householdId,memberId,"其他");
    }
    @Override
    @Transactional
    public void deleteFamilyarchives(String idCard) {
        Record oldRecord = recordMapper.findByIdcard(idCard);
        if(oldRecord.getType().equals("1")){
            familyarChivesMapper.deleteByHouseholdId(idCard);
            recordMapper.updateRecordType(idCard,"0");
        }
        else {
            familyarChivesMapper.deleteByMemberId(idCard);
            recordMapper.updateRecordType(idCard,"0");
        }
    }

    @Override
    public List<FamilyarChivesRecordList> findByIdCardAllListNo(List<String> Idcard) {
        return null;
    }

    @Override
    public List<FamilyarChivesRecordList> findByIdCardAllListNo(List<String> Idcard, String idNamePhone) {
        List<FamilyarChivesRecordList> familyarChivesRecordListsNo1 = new ArrayList<>();
        List<FamilyarChivesRecordList> familyarChivesRecordListsNo2 = new ArrayList<>();
        List<RecordList> recordLists = recordListService.getRecordList();
        List<Record> RecordList = getRecordList.findAllRecords();
        for (RecordList recordList : recordLists) {
            if (!Idcard.contains(recordList.getIdcard())) {
                for (Record record : RecordList){
                    if (record.getIdcard().contains(recordList.getIdcard())){
                        FamilyarChivesRecordList familyarChivesRecordList = new FamilyarChivesRecordList(
                                recordList.getIdcard(),
                                recordList.getName(),
                                recordList.getSex(),
                                recordList.getAge(),
                                recordList.getListenername(),
                                recordList.getPhone(),
                                recordList.getStatus(),
                                record.getType()
                        );
                        familyarChivesRecordListsNo1.add(familyarChivesRecordList);
                    }
                }
            }
        }
        for (FamilyarChivesRecordList familyarChivesRecordList : familyarChivesRecordListsNo1){
            if ((familyarChivesRecordList.getIdcard().contains(idNamePhone) ||
                    familyarChivesRecordList.getName().contains(idNamePhone) ||
                    familyarChivesRecordList.getPhone().equals(idNamePhone))) {
                familyarChivesRecordListsNo2.add(familyarChivesRecordList);
            }
        }
        return familyarChivesRecordListsNo2;
    }

    @Override
    public FamilyarSigning familySigningInformation(String Idcard, int doctorId) {
        FamilyarChivesList  familyarChivesList = findByIdCardAllFamilyList(Idcard);
        Doctor doctor = doctorMapper.findByDoctorId(doctorId);
        FamilyarSigning familyarSigningList = new FamilyarSigning(
                familyarChivesList.getHouseholdName(),
                familyarChivesList.getHouseholdSex(),
                familyarChivesList.getHomeAddress(),
                familyarChivesList.getHouseholdId(),
                familyarChivesList.getHouseholdPhone(),
                doctor.getName(),
                doctor.getOrganization(),
                familyarChivesList.getFamilyMembers()
        );
        return familyarSigningList;
    }

    @Override
    public FamilyarChivesList findByIdCardAllFamilyList(String Idcard) {
        FamilyarChivesList familyarChivesList = new FamilyarChivesList();
        List<FamilyarChivesRecordList> familyarChivesRecordLists =  new ArrayList<>();
        Record record = recordMapper.findByIdcard(Idcard);
        if(record.getType().equals("1")){
            List<FamilyarChives> familyarChives = familyarChivesMapper.findByHouseholdId(Idcard);
            List<FamilyarChives> familyarChiveslist = familyarChivesMapper.findByHouseholdId(familyarChives.get(0).getHouseholdId());
            familyarChivesList.setHouseholdName(record.getName());
            familyarChivesList.setHouseholdSex(record.getSex());
            familyarChivesList.setHomeAddress(record.getResidence()+record.getDetailedresidence());
            familyarChivesList.setHouseholdId(Idcard);
            RecordList recordList1 = getRecordList.getRecordByIdcard(familyarChives.get(0).getHouseholdId());
            mergefindByIdCardAllFamilyList(familyarChivesList, familyarChivesRecordLists, familyarChiveslist, recordList1);
        }else {
            FamilyarChives familyarChives = familyarChivesMapper.findByMemberId(Idcard);
            List<FamilyarChives> familyarChiveslist = familyarChivesMapper.findByHouseholdId(familyarChives.getHouseholdId());
            Record record2 = recordMapper.findByIdcard(familyarChives.getHouseholdId());
            familyarChivesList.setHouseholdName(record2.getName());
            familyarChivesList.setHouseholdSex(record2.getSex());
            familyarChivesList.setHomeAddress(record2.getResidence()+record.getDetailedresidence());
            familyarChivesList.setHouseholdId(familyarChives.getHouseholdId());
            RecordList recordList1 = getRecordList.getRecordByIdcard(familyarChives.getHouseholdId());
            mergefindByIdCardAllFamilyList(familyarChivesList, familyarChivesRecordLists, familyarChiveslist, recordList1);
        }
        return familyarChivesList;
    }

    private void mergefindByIdCardAllFamilyList(FamilyarChivesList familyarChivesList, List<FamilyarChivesRecordList> familyarChivesRecordLists, List<FamilyarChives> familyarChiveslist, RecordList recordList1) {
        familyarChivesList.setHouseholdPhone(recordList1.getPhone());
        FamilyarChivesRecordList familyarChivesRecordList1 = new FamilyarChivesRecordList(
                recordList1.getIdcard(),
                recordList1.getName(),
                recordList1.getSex(),
                recordList1.getAge(),
                recordList1.getListenername(),
                recordList1.getPhone(),
                recordList1.getStatus(),
                "户主"
        );
        for(FamilyarChives familyarChives1 : familyarChiveslist){
            if(familyarChives1.getRelationship().equals("户主")){
                continue;
            }
            else{
                RecordList recordList2 = getRecordList.getRecordByIdcard(familyarChives1.getMemberId());
                FamilyarChivesRecordList familyarChivesRecordList2 = new FamilyarChivesRecordList(
                        recordList2.getIdcard(),
                        recordList2.getName(),
                        recordList2.getSex(),
                        recordList2.getAge(),
                        recordList2.getListenername(),
                        recordList2.getPhone(),
                        recordList2.getStatus(),
                        familyarChives1.getRelationship()
                );
                familyarChivesRecordLists.add(familyarChivesRecordList2);
            }
        }
        familyarChivesRecordLists.add(familyarChivesRecordList1);
        familyarChivesList.setFamilyMembers(familyarChivesRecordLists);
    }

    @Override
    @Transactional
    public void addFamilyarchives(String idCard) {
        recordMapper.updateRecordType(idCard,"1");
        familyarChivesMapper.insertFamilyarchives(idCard,idCard,"户主");
    }

    @Override
    public void updateFamilyarchives(String idCard, String relationship) {
        familyarChivesMapper.UpdateByMemberId(idCard,relationship);
    }
}
